/**
 * 影像模型
 */
import LayerModel from "../LayerModel";
import store from "@/store/store";

export default class ImageryLayerModel extends LayerModel {

  constructor(layerInfo, viewer = window.viewer){
    super(layerInfo);
    this.layerInfo = layerInfo;
    this.viewer = viewer;
  }

  addLayer(){

    let index = this.layerInfo.index || 0;
    setTimeout(() => {

      if (this.layerInfo.subdomain) { // 通过子域加载
        this.addLayerBySubdomain();
      } else {
        this.addLayerNoSubdomain();
      }

    }, index);

  }

  // 添加图层：使用子域配置
  addLayerBySubdomain(){
    let maximumLevel = this.layerInfo.maximumLevel || 18;
    let minimumLevel = this.layerInfo.minimumLevel || 0;

    // console.log(this.layerInfo.subdomain.subdomainConfig.url);
    let imageryLayerProvider = new Cesium.SuperMapImageryProvider({
      url: this.layerInfo.subdomain.subdomainConfig.url,
      subdomains: this.layerInfo.subdomain.subdomainConfig.subdomains,
      name: this.layerInfo.subdomain.name,
      maximumLevel
      // minimumLevel: minimumLevel
    });

    this.addImageryProvider(imageryLayerProvider);
  }

  // 添加图层：没有子域配置
  addLayerNoSubdomain(){

    let url = this.layerInfo.url;

    // 判断服务是否已经加载
    if (store.state.viewerLayers[url]) {
      console.error(`ImageryLayerModel添加图层错误，图层名称为：${this.layerInfo.name}。\n原因：该影像图层已经存在。\nurl = ${url}。`);
      return;
    }

    let imageryLayerProvider = new Cesium.SuperMapImageryProvider({
      url: url,
      name: this.layerInfo.name,
      maximumLevel: this.layerInfo.maximumLevel || 18
      // minimumLevel: this.layerInfo.minimumLevel || 1
    });

    this.addImageryProvider(imageryLayerProvider);
  }

  //  将影响提供者添加到影响图层
  addImageryProvider(imageryLayerProvider){

    // 图层加载完成
    imageryLayerProvider.readyPromise.then(() => {

      let index = this.layerInfo.index;
      let imageryLayers = viewer.imageryLayers;
      let layer;
      if (imageryLayers.length >= index) {
        layer = imageryLayers.addImageryProvider(imageryLayerProvider, index);
      } else {
        layer = imageryLayers.addImageryProvider(imageryLayerProvider);
      }

      layer.alpha = this.layerInfo.alpha || 1;// 透明度
      layer.show = this.layerInfo.show || false; // 图层是否显示

      // 保存当前图层对象到模型
      this.childLayers = layer;

      // 将对象添加到图层列表中
      store.state.viewerLayers[this.layerInfo.url] = this;

    }).otherwise(errInfo => {
      console.error(`ImageryLayerModel添加图层错误，图层名称为：${this.layerInfo.name}。原因：${errInfo}。url = ${this.layerInfo.url}。`);
    });
  }

  /**
   * 添加天地图
   * @param layerType IMG(影像)、VEC(矢量)、CIA(注记)
   * @param token 天地图token
   */
  addTiandituImagery(layerType = "IMG", tiandituToken = store.state.tiandituToken){
    if (!layerType) {
      return;
    }

    let tiandituImageryProvider;

    switch (layerType) {
      case "VEC":
        tiandituImageryProvider = new Cesium.TiandituImageryProvider({
          token: tiandituToken
        });
        break;
      case "CIA":
        tiandituImageryProvider = new Cesium.TiandituImageryProvider({
          mapStyle: Cesium.TiandituMapsStyle.CIA_C, //天地图全球中文注记服务（经纬度投影）
          token: tiandituToken
        });
        break;
      case "IMG":
      default:
        tiandituImageryProvider = new Cesium.TiandituImageryProvider({
          token: tiandituToken
        });
    }

    let layer = this.viewer.imageryLayers.addImageryProvider(tiandituImageryProvider);
    // 保存当前图层对象到模型
    this.childLayers = layer;

    this.layerInfo = {
      url: `tianditu_${layerType}`
    };
    // 将对象添加到图层列表中
    store.state.viewerLayers[`tianditu_${layerType}`] = this;
    // console.log(`layer`, layer);
    // layer.show = false;
    // this.addImageryProvider(tiandituImageryProvider);
  }

  /**
   * 删除所有图层
   */
  static removeAll(destroy = true, viewer = window.viewer){
    viewer.imageryLayers.removeAll(destroy);
  }

  // 删除图层
  removeLayer(){

    scene.imageryLayers.remove(this.childLayers);

    if (store.state.viewerLayers[url]) {
      store.state.viewerLayers[url] = undefined;
      delete store.state.viewerLayers[url];
    }

    return true;
  }

  // 飞行
  flyTo(){

    let isFly = super.flyTo(); // 按配置的坐标飞行

    // 没有在配置文件中定义坐标位置坐标
    if (!isFly) {
      let layer = this.childLayers;
      if (layer) {
        viewer.flyTo(layer);
      }
    }
  }

  // 设置图层的可见性
  setVisible(isVisible){
    let layer = this.childLayers;
    if (layer) {
      layer.show = isVisible;
    }
  }
}
